using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Employers_postjob : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session.Contents["rcuserid"] == null)
        {
            Response.Redirect("login.aspx");
        }
        
        if (!IsPostBack)
        {
            FillDropDownList();
            try
            {
                Page page =this.PreviousPage;
                
                if (page != null) 
                {
                    TextBox textBox = (TextBox)page.FindControl("txtJobID");
                    string jobID  = textBox.Text;
                    ShowDetails(jobID);                     
                    txtJobID.Text = jobID;                     
                }
                 
                    
                if(txtJobID.Text != "")
                    btnSave.Text = "Update";
                    
                Literal1.Text += "  <input type=button " +
                        "value='Back' " +
                        "onclick =\"window.history.go(-1);\">";
            }
            catch (Exception ex)
            {
                litError.Text = ex.Message;
            }
        }
    }
    DataTable GetDataTable(string jobID)
    {
        DataTable dataTable = null;
        try
        {
            SqlServerProvider cls = new SqlServerProvider("");
            cls.CommandText = "SPViewJob";
            cls.ParameterCollection = new string[2] {"@RecruiterID", "@JobID" };
            cls.ValueCollection = new string[2] {Session.Contents["rcuserid"].ToString(), jobID };
            cls.CommandType = CommandType.StoredProcedure;
            dataTable = cls.GetDataTable();            
        }
        catch (Exception ex)
        {
            litError.Text = ex.Message;
        }
        return dataTable;
    }
    void FillDropDownList()
    {
        ControlHelper controlHelper = new ControlHelper("");
        controlHelper.CommandText =
        "select * from tblJobCategories; " +
        "Select * from tblJobIndustries;" +
        "select * from tblProvinces;"+
        "select * from tblJSCertificates";
        controlHelper.InitProperties();
        controlHelper.FillDataTableToDropDownList(
        ddlCategory, ddlIndustry, ddlProvince, ddlCertificate,
        "JobCategoryName", "JobIndustryName", "ProvinceName", "CertificateName",  
        "JobCategoryID", "JobIndustryID", "ProvinceID", "CertificateID", 
        "", "", "", "");
    }
    void ShowDetails(string jobID)
    {
        DataTable dataTable = GetDataTable(jobID);
        if(dataTable.Rows.Count > 0 )
        {
            txtJobTitle.Text = Convert.ToString(dataTable.Rows[0]["JobTitle"]);
            txtJobNo.Text = Convert.ToString(dataTable.Rows[0]["JobNo"]);
            txtJobQuantity.Text = Convert.ToString(dataTable.Rows[0]["RequiredNumber"]);
            txtSummary.Text = Convert.ToString(dataTable.Rows[0]["JobSummary"]);
            ddlWorkType.SelectedValue = Convert.ToString(dataTable.Rows[0]["WorkingTypeID"]);
            ddlExperienceLevel.SelectedValue = Convert.ToString(dataTable.Rows[0]["ExperienceLevelID"]);
            txtMinSalary.Text = Convert.ToString(dataTable.Rows[0]["SalaryFrom"]);
            txtMaxSalary.Text = Convert.ToString(dataTable.Rows[0]["SalaryTo"]);
            ddlCurrency.SelectedValue = Convert.ToString(dataTable.Rows[0]["Currency"]);
            chkShowSalary.Checked = (Convert.ToString(dataTable.Rows[0]["ShownSalary"]) == "True"?true:false);
            chkNegotive.Checked = (Convert.ToString(dataTable.Rows[0]["SalaryNegotive"]) == "True"?true:false);
            txtCloseDate.Text = Convert.ToString(dataTable.Rows[0]["ClosedDate"]);
            chkLogo.Checked = (Convert.ToString(dataTable.Rows[0]["CompanyLogo"]) == "True" ? true : false);
            txtAdv.Text = Convert.ToString(dataTable.Rows[0]["AdvName"]);
            ddlIndustry.SelectedValue = Convert.ToString(dataTable.Rows[0]["JobIndustryID"]);
            ddlCategory.SelectedValue = Convert.ToString(dataTable.Rows[0]["JobCategoryID"]);
            ddlCertificate.SelectedValue = Convert.ToString(dataTable.Rows[0]["CertificateID"]);
            txtLocation.Text = Convert.ToString(dataTable.Rows[0]["WorkLocation"]);
            ddlProvince.SelectedValue = Convert.ToString(dataTable.Rows[0]["ProvinceID"]);
            ddlTemplate.SelectedValue = Convert.ToString(dataTable.Rows[0]["TemplateID"]);
            chkOnline.Checked = (Convert.ToString(dataTable.Rows[0]["EnableApplyOnline"]) == "True"?true:false);
            txtURL.Text = Convert.ToString(dataTable.Rows[0]["OnlyApplyURL"]);
            txtEmail.Text = Convert.ToString(dataTable.Rows[0]["ContactEmail"]);
            txtContact.Text = Convert.ToString(dataTable.Rows[0]["ContactPerson"]);
            txtTel.Text = Convert.ToString(dataTable.Rows[0]["ContactTel"]);            
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            SqlServerProvider cls = new SqlServerProvider("");
            cls.ParameterCollection = new string[27]
            {"@JobID","RecruiterID","@JobTitle","@JobNo","@RequiredNumber",
            "@JobSummary","@WorkingTypeID",
            "@ExperienceLevelID","@SalaryFrom","@SalaryTo",
            "@Currency","@ShownSalary","@SalaryNegotive",
            "@ClosedDate","@CompanyLogo","@AdvName",
            "@JobIndustryID","@JobCategoryID","@CertificateID",
            "@WorkLocation","@ProvinceID","@TemplateID",
            "@EnableApplyOnline","@OnlyApplyURL",
            "@ContactEmail","@ContactPerson","@ContactTel"};
            cls.ValueCollection = new string [27] 
            {   "0", Session.Contents["rcuserid"].ToString(),
                txtJobTitle.Text, txtJobNo.Text, 
                txtJobQuantity.Text,txtSummary.Text, 
                ddlWorkType.SelectedValue, 
                ddlExperienceLevel.SelectedValue,
                txtMinSalary.Text, txtMaxSalary.Text,
                ddlCurrency.SelectedValue,
                ((chkShowSalary.Checked)?"1":"0"),
                ((chkNegotive.Checked)?"1":"0") ,
                txtCloseDate.Text,
                ((chkLogo.Checked)?"1":"0"),
                txtAdv.Text, ddlIndustry.SelectedValue,
                ddlCategory.SelectedValue,
                ddlCertificate.SelectedValue,
                txtLocation.Text,
                ddlProvince.SelectedValue,
                ddlTemplate.SelectedValue,
                ((chkOnline.Checked )?"1":"0"),
                txtURL.Text, txtEmail.Text ,
                txtContact.Text, txtTel.Text};
            if(btnSave.Text == "Update")
            {
                cls.ValueCollection[0] = txtJobID.Text;
            }
            if(cls.SavePostJob())
                Response.Redirect("jobmanager.aspx");;
        }
        catch (Exception ex)
        {
            litError.Text = ex.Message;
        }
    }
}
